package kfs.light.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import kfs.light.arduino.Arduino;
import kfs.light.arduino.ArduinoConsole;
import kfs.light.screen.Screen;
import kfs.light.screen.Zone;
import kfs.light.screen.ZoneBuilder;
import kfs.light.ui.MainFrame;
import kfs.light.ui.NewZoneFileFrame;

public class Starter {

	private static final String fPath = "/.config/kfsl/zones.dat";
	public static Core core;
	public static File configurationFile;

	public static void main(String[] args) {

		String homePath = System.getProperty("user.home");
		String path = homePath.concat(fPath);
		List<String> zones = new ArrayList<String>();
		configurationFile = new File(path);
		if (!configurationFile.exists()) {
			try {
				System.out.println("Can't access file");
				System.out.println("Creating new one");
				configurationFile.createNewFile();
				System.out.println("Success");
				System.out.println("Asking new zone data");
				NewZoneFileFrame zoneFrame = new NewZoneFileFrame();
				zoneFrame.setVisible(true);
				zoneFrame.pack();
				while(!NewZoneFileFrame.finished){
					Thread.sleep(1000);
				}
				zoneFrame.dispose();
				System.out.println("Finished");				
			} catch (IOException e) {
				System.out.println("Can't create file");
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		FileInputStream is;
		try {
			is = new FileInputStream(configurationFile);
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			while (br.ready()) {
				zones.add(br.readLine());
			}
			is.close();
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Zone count:" + zones.size());
		if (zones.size()>0) {
			// Reading zone data;

			HashMap<String, Zone> zoneMap = new HashMap<String, Zone>();
			List<String> keyList = new ArrayList<String>();
			for (int i = 0; i < zones.size(); i++) {
				String zoneData = zones.get(i);
				System.out.println(zoneData);
				String[] split = zoneData.split("[ ]");
				String key = split[0];
				Zone zone = ZoneBuilder.buildZone(zoneData);
				keyList.add(key);
				zoneMap.put(key, zone);
			}
			// Defining screen;
			Screen scr = new Screen();
			scr.setZoneMap(zoneMap);
			scr.setZoneKeys(keyList);
			// Defining arduino module
			Arduino ard = new ArduinoConsole();

			// If arduino availiable setting up core ad launching loop

			core = new Core();
			Core.arduinoModule = ard;
			Core.screen = scr;
			Core.zoneList = keyList;

			try {
				UIManager.setLookAndFeel(UIManager
						.getSystemLookAndFeelClassName());
			} catch (ClassNotFoundException | InstantiationException
					| IllegalAccessException | UnsupportedLookAndFeelException e) {
				try {
					UIManager.setLookAndFeel(UIManager
							.getCrossPlatformLookAndFeelClassName());
				} catch (ClassNotFoundException | InstantiationException
						| IllegalAccessException
						
						| UnsupportedLookAndFeelException e1) {

					e1.printStackTrace();
				}
			}

			MainFrame app = new MainFrame();
			app.setVisible(true);
			app.pack();

		}
	}

}
